<?php

	/*
	 * The commands page
	 * Commands are used to have the SMS Display swf file do something: change settings or show sponsors.
	 *
	 * Variables:
	 *
	 * Returns: HTML page
	 *
	 */


	$script = '
	function delcommand(id){
		if(confirm("Are you sure you want to remove the command \'"+$("#tdsms"+id).text()+"\' which was set to be executed around "+$("#dt"+id).text()+"?")){
			$.get("'.$url_prefix.'ajax/deletecommand/", { id: id }, function(data){
				if(data == "ok"){
					$("#"+$("#tdsms"+id).parent().attr("id")).fadeOut();
				}else{
					alert(data);
				}
			});
		}
	}

	function loadstats(){
		$("#stats").parent().css("background","url('.$url_prefix.'/images/loadinv.gif) bottom right no-repeat");
		$("#stats").load("'.$url_prefix.'/ajax/loadstats/");
		$("#stats").parent().css("background","none");
	}

	$(document).ready(function(){
		$(".listdiv").hover(
			function(){ $(this).addClass("listdiv_hover"); },
			function(){ $(this).removeClass("listdiv_hover"); }
		);
		setInterval("loadstats()", 5000);
		loadcommandlist();
	});




		var TXTmaxID = 0;
		var TXTminID = 9999999;



		function loadcommandlist(){
			$("#smsloadlistindicator").css("display","inline");
			request = $.ajax({
				url: "'.$url_prefix.'ajax/loadcommandlist/&id="+TXTmaxID,
				processData: false,
				success: function(xml){
//					alert("'.$url_prefix.'ajax/loadcommandlist/&id="+TXTmaxID);
					$(request.responseXML).find("text").each(function(){
						var mynode = $(this);

						$("#commandlist tr").each(function(){
							if($(this).attr("id") < mynode.attr("timestamp")){
//								alert("Gonna insert right before "+$(this).attr("id"));
								$(this).before("<tr id=\""+mynode.attr("timestamp")+"\"><td id=\"txtstatus"+mynode.attr("id")+"\"><img src=\"'.$url_prefix.'images/status"+mynode.attr("status")+".png\" /></td><td id=\"dt"+mynode.attr("id")+"\">"+mynode.attr("dt")+"</td><td id=\"tdsms"+mynode.attr("id")+"\">"+mynode.text()+"</td><td style=\"text-align:right\"><a class=\"fb\"  href=\"javascript:delcommand("+mynode.attr("id")+")\"><img src=\"'.$url_prefix.'images/page_white_delete.png\" /></a></td></tr>");
								return false;
							}else{
//								alert("Oeps, "+$(this).attr("id")+" < "+mynode.attr("timestamp"));
							}
						});


						TXTmaxID = Math.max(TXTmaxID, $(this).attr("id"));
						if($(this).attr("id") > 0){
							TXTminID = Math.min(TXTminID, $(this).attr("id"));
						}







					});

					$(request.responseXML).find("status").each(function(){
						$("#"+$(this).attr("timestamp")).remove();
					});


					$("#smsinnerlist").jScrollPane({showArrows:true,scrollbarWidth:10, scrollbarMargin:5});

					$(".smslist tr").hover(
						function(){$(this).addClass("stripelight");},
						function(){$(this).removeClass("stripelight");}
					);
					$("#smsloadlistindicator").css("display","none");
					setTimeout("loadcommandlist()", 2000);

					makestatuslinks();
				}
			});

		}

		function makestatuslinks(){
			$("img[src$=\'status2.png\']").hover(
				function(){ this.src="'.$url_prefix.'images/statusR.png"; },
				function(){ this.src="'.$url_prefix.'images/status2.png"; }
			);
			$("img[src$=\'status2.png\']").click(function(){
				if(confirm("Are you sure you want this text to appear again?")){
					$.get("'.$url_prefix.'ajax/textreset/&id="+$(this).parent().attr("id").substr(9));
					this.src="'.$url_prefix.'images/status3.png";
				}
			});
			$("img[src$=\'status1.png\']").hover(
				function(){ this.src="'.$url_prefix.'images/statusR.png"; },
				function(){ this.src="'.$url_prefix.'images/status1.png"; }
			);
			$("img[src$=\'status1.png\']").click(function(){
				if(confirm("Are you sure you want this text to appear again?")){
					$.get("'.$url_prefix.'ajax/textreset/&id="+$(this).parent().attr("id").substr(9));
					this.src="'.$url_prefix.'images/status3.png";
				}
			});


		}

		function savecommand(){
			var url = "'.$url_prefix.'ajax/savecommand/&type="+$("#newcommand #comtype").val()+"&pars="+$("#newcommand #pars").val()+"&frst="+$("#newcommand #first").val()+"&rpt="+$("#newcommand #rept").val();
			$.get(url, function(data){
				$("#saveOK").fadeIn(100).fadeTo(1000,1).fadeOut(1000);
			});
		}

		function preparepars(){
			$("#newcommand #pars").val($("#newcommand #comtype option:selected()").attr("class"));
			if($("#newcommand #comtype option:selected()").attr("class") != ""){
				$("#newcommand #pars").focus();
			}else{
				$("#newcommand #first").focus();
			}
		}
	';



	htmlheader($script);

	echo '

	<div id="lpart">
	';


	echo '
		<h3>Make new command</h3>
		<div class="bigboxcover" style=""><div id="saveOK" style="display:none; position: absolute; top: 27px; left: 1px; background: #192839; height: 120px; width: 300px;">OK, command has been saved...</div><div id="newcommand" style="margin:3px;">
		<table style="width:100%">
			<tr><td>Command type:</td><td><select id="comtype" name="comtype" onChange="javascript:preparepars()"><option value="">Choose any</option>
				<optgroup label="Sponsors">
				<option value="SPONSORS">Enter name as parameter:</option>
				';
				$tmp = opendir("sponsors/");
				while(false !== ($file = readdir($tmp))){
					if(substr($file,0,1) != "."){
						echo '				<option value="SPONSORS '.str_replace(".swf","",$file).'">'.str_replace(".swf","",$file)."</option>\n";
					}
				}

				echo '
				</optgroup>
				<optgroup label="Settings">
					<option value="SETTINGS bgcolor" class="0x">SMS Background color</option>
					<option value="SETTINGS fgcolor" class="0x">SMS Foreground color</option>
					<option value="SETTINGS pubbgcolor" class="0x">TXT Background color</option>
					<option value="SETTINGS pubfgcolor" class="0x">TXT Foreground color</option>
					<option value="SETTINGS smshidedelay" class="3">SMS Hide delay (sec)</option>
					<option value="SETTINGS txthidedelay" class="3">TXT Hide delay (sec)</option>
					<option value="SETTINGS downspeed" class="10">Speed of texts going down</option>
					<option value="SETTINGS phonenumber" class="SMS">SMS Number (incl SMS)</option>
					<option value="SETTINGS smsnrcolor" class="0x">SMS Number color</option>
					<option value="SETTINGS sponsors_local" class="">Sponsors local base</option>
					<option value="SETTINGS sponsors_remote" class="http://127.0.0.1/SMS/sponsors/">Sponsors remote base</option>
					<option value="SETTINGS RELOAD" class="">Reload defaults</option>
				</optgroup>
				</select></td></tr>
			<tr><td>Parameters:</td><td><input type="text" style="width: 100%; border:0px; border-bottom:1px solid #000000; background:transparent" id="pars" /></td></tr>
			<tr><td>First execution:</td><td><input id="first" value="'.date("Y-m-d H:i",time()).'" style="width: 115px; border:0px; border-bottom:1px solid #000000; background:transparent" /></td></tr>
			<tr><td>Repeat every:</td><td><a style="float:right; padding-top:5px;" href="javascript:savecommand()"><img src="'.$url_prefix.'images/disk.png" /></a><select id="rept"><option value="0">No repeat</option>';
			for($i=0;$i<=60;($i>=10)?$i+=5:$i++){ echo '<option value="'.$i.'">'.$i.' minutes</option>';  }
		echo '
			</select></td></tr>
		</table>
		</div></div>



		<h3>Statistics</h3>
		<div class="bigboxcover" style=""><div id="stats" style="margin: 3px;">
		</div></div>
	</div>
	';


	echo '
		<div id="rpart">
			<table class="smslistcover striped" style="width:100%">
				<tr style="height:30px;"><th>Commands List</th><th style="width:25px; text-align:center"><img src="'.$url_prefix.'/images/load.gif" style="" id="smsloadlistindicator" /></th></tr>
				<tr><td colspan="2" style="padding:0px;">
				<div id="smsinnerlist" style="height:400px; overflow:hidden">
					<table class="smslist" id="commandlist" style="width:100%">
	';



	echo '
			<tr id="0"><td style="width: 20px;"></td><td style="font-size: 1px; width: 60px">&nbsp;</td><td style="font-size: 1px;">&nbsp;</td><td style="width:20px;"></td></tr>
			</table>
			</td></tr></table>
		</div>

		<div class="editbox" style="height: 130px;"></div>
	';



	htmlfooter();
?>